#include "GameMenu.h"
#include "ImgMgr.h"

GameMenu::GameMenu()
{

}

void GameMenu::init(ImgMgr *imageManager)
{
    m_active = true;
    m_mainMenu = true;
    m_aboutMenu = false;
    m_selectedButton = 0;
    m_font.loadFromFile("ressource/font.ttf");

    m_header = imageManager->getSprite(65, 79);
    m_header.setPosition(230 - m_header.getGlobalBounds().width * 3 / 2, 0);
    m_header.setScale(3, 3);

    m_play.setFont(m_font);
    m_about.setFont(m_font);
    m_textAbout.setFont(m_font);

    m_play.setCharacterSize(52);
    m_about.setCharacterSize(52);
    m_about.setCharacterSize(52);

    m_play.setString("Play !");
    m_about.setString("About");
    m_textAbout.setString("This game was made by Tintounn. \nThis is my first participation \nto the Ludum Dare. \n\nYou are a pill and \nthe goal is to destroy any bacteria \nbeing the fastest \nof different levels of human corp. \n\n\nThe arrow keys for move \nand A for fire. \n\n\nGood Game :)");

    m_play.setPosition(230 - m_play.getGlobalBounds().width / 2, 230);
    m_about.setPosition(230 - m_about.getGlobalBounds().width / 2, 230 + m_play.getGlobalBounds().height * 2);
    m_textAbout.setPosition(230 - m_textAbout.getGlobalBounds().width / 2, 0);

    m_play.setColor(sf::Color::White);
    m_about.setColor(sf::Color::White);
    m_textAbout.setColor(sf::Color::White);

    m_wallShadow.setSize(sf::Vector2f(460, 460));
    m_wallShadow.setFillColor(sf::Color::Color(0, 0, 0, 175));

}

void GameMenu::setActive(const bool active)
{
    m_active = true;
}

bool GameMenu::getActive() const
{
    return m_active;
}

void GameMenu::event()
{
    if(m_clockSelect.getElapsedTime().asMilliseconds() > 100)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            m_selectedButton = 0;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            m_selectedButton = 1;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
        {
            if(m_mainMenu)
            {
                if(m_selectedButton == 0)
                {
                    m_active = false;
                }
                else
                {
                    m_mainMenu = false;
                    m_aboutMenu = true;
                }
            }
            else
            {
                m_mainMenu = true;
                m_aboutMenu = false;
            }
        }
        m_clockSelect.restart();
    }
}

void GameMenu::render(sf::RenderWindow *app)
{
    app->draw(m_wallShadow);
    if(m_mainMenu)
    {
        app->draw(m_header);
        if(m_selectedButton == 0)
        {
            m_play.setColor(sf::Color::Red);
            m_about.setColor(sf::Color::Color(67, 67, 67));
        }
        else
        {
           m_play.setColor(sf::Color::Color(67, 67, 67));
           m_about.setColor(sf::Color::Red);
        }
        app->draw(m_play);
        app->draw(m_about);
    }
    else
    {
        app->draw(m_textAbout);
    }
}

